from datetime import datetime
from decimal import Decimal
from typing import Optional

from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel
from pydantic_validation_decorator import NotBlank, Size

from module_admin.annotation.pydantic_annotation import as_query


class TransferModel(BaseModel):
    """
    银证转账表对应pydantic模型
    """
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)

    id: Optional[int] = Field(default=None, description='主键ID')
    transfer_date: Optional[datetime] = Field(default=None, description='日期')
    transfer_time: Optional[str] = Field(default=None, description='时间')
    amount: Optional[Decimal] = Field(default=None, description='金额')
    operation: Optional[str] = Field(default=None, description='操作')
    account_no: Optional[str] = Field(default=None, description='账户')
    create_time: Optional[datetime] = Field(default=None, description='创建时间')
    update_time: Optional[datetime] = Field(default=None, description='更新时间')

    @NotBlank(field_name='transfer_date', message='日期不能为空')
    def get_transfer_date(self):
        return self.transfer_date

    @Size(field_name='transfer_time', min_length=0, max_length=10, message='时间长度不能超过10个字符')
    def get_transfer_time(self):
        return self.transfer_time

    @NotBlank(field_name='amount', message='金额不能为空')
    def get_amount(self):
        return self.amount

    @NotBlank(field_name='operation', message='操作不能为空')
    def get_operation(self):
        return self.operation

    @NotBlank(field_name='account_no', message='账户不能为空')
    def get_account_no(self):
        return self.account_no

    def validate_fields(self):
        self.get_transfer_date()
        self.get_transfer_time()
        self.get_amount()
        self.get_operation()
        self.get_account_no()


class TransferQueryModel(TransferModel):
    """
    银证转账不分页查询模型
    """
    account_nos: Optional[str] = Field(default=None, description='所属证券账号列表')
    account_no_list: Optional[list] = Field(default=None, description='所属证券账号列表')


@as_query
class TransferPageQueryModel(TransferQueryModel):
    """
    银证转账分页查询模型
    """

    page_num: int = Field(default=1, description='当前页码')
    page_size: int = Field(default=10, description='每页记录数')


class DeleteTransferModel(BaseModel):
    """
    删除银证转账模型
    """

    model_config = ConfigDict(alias_generator=to_camel)

    ids: str = Field(description='需要删除的主键ID')
